//
//  BaseResponse.swift
//  AJiTai
//
//  Created by BO ZHOU on 2020/5/16.
//  Copyright © 2020 BO ZHOU. All rights reserved.
//

import Foundation
import HandyJSON
import Alamofire

/**
 * 模型基类  创建模型的时候继承   可以不用import HandyJSON
 * 在创建model的时候, 如果data返回的是string, bool, int等可以不用写属性, 直接写:
 * Class DemoModel: BaseModel {} 即可, 在request回调里面, 通过response.string, .bool获取返回值
 */
open class BaseModel: HandyJSON, ResponseProtocol {
    required public init () {}
}

open class BaseResponse: HandyJSON {
    
    var code: Int = 0
    var msg: String = ""
    
    public required init() {

    }
}


open class DataResponse<T: HandyJSON>: BaseResponse {
    var data: T = T()
}

open class ListDataResponse<T: HandyJSON>: BaseResponse {
    var data: [T] = []
}

open class StringDataResponse: BaseResponse {
    var data: String = ""
}

open class BoolDataResponse: BaseResponse {
    var data: Bool = false
}

open class IntDataResponse: BaseResponse {
    var data: Int = 0
}

open class DoubleDataResponse: BaseResponse {
    var data: Double = 0
}

open class KVDataRespone<T: HandyJSON>: BaseResponse {
    var data: T?
}

public protocol ResponseProtocol {
   
}

// MARK: - APIModel request扩展, 限制只有遵守了ResponseProtocol的HandyJSON类才扩展request
public extension ResponseProtocol where Self: HandyJSON {
    
    
    // MARK: API
    /**
     返回data
     */
    @discardableResult static func request(url: String,
                                           method: HTTPMethod = .post ,
                                           params: [String:Any]?,
                                           showLoading: Bool = true,
                                           loadingInView: UIView? = nil,
                                           showError: Bool = true,
                                           errorInView: UIView? = nil,
                                           success:((Bool, ObjDataResponse<Self>?)->())?) -> DataRequest? {

        return BaseApiManager.request(withPath: url, method: method, parameters: params, showLoading: showLoading, loadingInView: loadingInView, showError: showError, errorInView: errorInView, callBackBlock: { (isSuccess, response) in
            
            success?(isSuccess, ObjDataResponse<Self>(with: response))
        })
    }
    
    /**
     * 不按照baseUrl来
     */
    @discardableResult static func request(absoluteUrl: String, method: HTTPMethod = .post ,
                                           params: [String:Any]?,
                                           showLoading: Bool = true,
                                           loadingInView: UIView? = nil,
                                           showError: Bool = true,
                                           errorInView: UIView? = nil,
                                           success:((Bool, ObjDataResponse<Self>?)->())?) -> DataRequest? {

        return BaseApiManager.request(withAbsolutePath: absoluteUrl, method: method, parameters: params, showLoading: showLoading, loadingInView: loadingInView, showError: showError, errorInView: errorInView, callBackBlock: { (isSuccess, response) in
            
            success?(isSuccess, ObjDataResponse<Self>(with: response))
        })
    }
}





